Component({
	properties: {
		matches: {
			type: Array,
			value: []
		},
		"placeholder": {
			type: String,
			value: ''
		},
		"general": {
			type: Boolean,
			value: true
		},
		"address": {
			type: Array,
			value: []
		}
	},
	data: {
		inputShowed: false,
		inputVal: "",
		selected: {},  // 当前选中筛选项
		subSelected: {},  // 当前二级菜单选中筛选项
		currentMenu: {},  // 当前选择的下拉菜单
		currentSelect: {}, // 当前选中所有集合
		menuTitle: {},  // 当前筛选项显示
		menuState: false,  // 菜单状态默认收起
		subMenuState: false,  // 二级菜单默认收起
		currentSubmenu: false,  // 当前选中二级菜状态
		more: {},  // 更多选择中的选项
		levelOneIndex: -1,  // 二级专业菜单
		levelTwoIndex: -1,  // 二级专业菜单
		levelThreeIndex: -1,  // 三级专业菜单
		currentall: { school: true, majoy: true, address: true, classType: true, schoolType: true, bkType:true}
	},
	methods: {
		showInput: function () {
			this.setData({
				inputShowed: true
			});
		},
		hideInput: function () {
			this.setData({
				//inputVal: "",
				inputShowed: false
			});
		},
		clearInput: function () {
			this.setData({
				inputVal: ""
			});
		},
		inputTyping: function (e) {
			this.setData({
				inputVal: e.detail.value
			});
		},
		search(e) {
			this.triggerEvent('search', this.data.inputVal);
			this.setData({
				//inputVal: '',
				inputShowed: false,
				currentMenu: {},  // 当前选择的下拉菜单
				selected:{},
				levelOneIndex: -1,  // 二级专业菜单
				levelTwoIndex: -1,  // 二级专业菜单
				levelThreeIndex: -1,  // 三级专业菜单
				currentall: { school: true, majoy: true, address: true, classType: true, schoolType: true, bkType: true }
			
			});
		},
		// 筛选功能
		cancelFilter(e) {
			this.setData({
				menuState: false
			})
			this.triggerEvent('onmask')
		},
		menuDown(event) {  // 展示下拉框
			this.setData({
				currentMenu: event.currentTarget.dataset.type || this.data.currentMenu,  // 菜单类型
				menuState: true
			})
			this.triggerEvent('menuDown', this.data.currentMenu)
		},
		menuClose(){
			this.setData({
				menuState: false,
			})
		},
		current(e){  // 获取当前筛选项
		
			let typeid = e.currentTarget.dataset.typeid; // 筛选类型
			let val = e.currentTarget.dataset.val;
			let length = e.currentTarget.dataset.length || 0;  // 是否有二级菜单
			let selected = this.data.selected;
			let currentall = this.data.currentall;
			let menuTitle = this.data.menuTitle;
			let index = e.currentTarget.dataset.index;
			let all = e.currentTarget.dataset.alltype

			index == "-1" ? currentall[all] = true : currentall[all] = false;
			
			if (length > 0){
				this.setData({ 
					levelOneIndex: index, 
					levelTwoIndex: -1 
				})
			}
			if (length == -1 && this.data.currentMenu == "majoy"){
				this.setData({
					levelOneIndex: -1,
				})
			}
			
			index == "-1" ? selected[this.data.currentMenu] = '' :selected[this.data.currentMenu] = typeid;
			menuTitle[this.data.currentMenu] = (val == -1) ? '' : val
			
			if (length > 0 || (length == -1 && this.data.currentMenu =="majoy")){
				this.setData({
					subMenuState: true,
					menuTitle,
					selected,
					currentall
				})
				return false
			}
			this.setData({
				menuState: false,
				menuTitle,
				selected,
				currentall
			})
			this.triggerEvent('current', this.data.selected)		
		},
		currentSub(e) {	
			let typeid = String(e.currentTarget.dataset.typeid); // 筛选类型
			let val = e.currentTarget.dataset.val;
			let subSelected = this.data.subSelected;
			let selected = this.data.selected
			let menuTitle = this.data.menuTitle;
			
			subSelected[this.data.currentMenu] = typeid;
			menuTitle[this.data.currentMenu] = val
			this.setData({
				menuState: false,
				currentSubmenu: true,
				menuTitle,
				selected: { ...selected, ...subSelected},
				subSelected
			})
			this.triggerEvent('current', this.data.selected)
			
		},
		onComplete(e){
			let selected = this.data.selected;
			let subSelected = this.data.subSelected;
	
			this.setData({
				menuState: false,
				currentSubmenu: true,
				selected: { ...selected, ...subSelected}	
			})
			this.triggerEvent('current', this.data.selected)
		},
		onReset(e){
			let selected = this.data.selected;
			let menuTitle = this.data.menuTitle;
			let currentall = this.data.currentall;
			let subSelected = this.data.subSelected;
			selected['majoy'] = '',
				currentall['majoy'] = true
			menuTitle['majoy'] = ''
			this.setData({
				levelOneIndex: -1,  // 二级专业菜单
				levelTwoIndex: -1,  // 二级专业菜单
				levelThreeIndex: -1,  // 三级专业菜单
				menuState: false,
				currentSubmenu: true,
				menuTitle,
				currentall,
				selected: { ...selected, ...subSelected}	
			})
			this.triggerEvent('current', this.data.selected)
		},
		onResetSearch(e){
			this.setData({
				inputVal: ""
			});
			this.triggerEvent('search', this.data.inputVal);
		},
		nextLevel(e) {
			let index = e.currentTarget.dataset.index;
			let typeid = e.currentTarget.dataset.typeid; // 筛选类型
			let val = e.currentTarget.dataset.val;
			let menuTitle = this.data.menuTitle;
			let selected = this.data.selected;
			let subSelected = this.data.subSelected;
			selected['majoy'] = typeid
			menuTitle[this.data.currentMenu] = val || ''
			this.setData({
				levelTwoIndex: index,  // 二级专业菜单
				menuTitle,
			})

			this.setData({ levelTwoIndex: index, levelThreeIndex: -1 })
		},
		threeLevel(e){
			let index = e.currentTarget.dataset.index
			let typeid = e.currentTarget.dataset.typeid; // 筛选类型
			let val = e.currentTarget.dataset.val;
			let menuTitle = this.data.menuTitle;
			menuTitle[this.data.currentMenu] = val || ''
			this.setData({
				levelThreeIndex: index,  // 三级专业菜单
				menuTitle,
			})

			let selected = this.data.selected;
			let subSelected = this.data.subSelected;
			selected['majoy'] = typeid;

			this.setData({ levelThreeIndex: index })
		}
	}
})
